Writing Reusable Steps with vars/
The vars/ directory is used to define global, reusable pipeline steps.
These steps are callable directly from a Jenkinsfile and are the most common entry point for shared libraries.
What Goes into vars/​
Use vars/ for:
- High-level pipeline steps
- Reusable stages or stage groups
- Thin orchestration logic
Avoid:
- Heavy business logic
- Complex class hierarchies
Naming Rules​
- Filename = step name
- Must be a valid Groovy identifier
- Case-sensitive
Example:
vars/
└── buildAndTest.groovy
Callable as:
buildAndTest()
Mandatory call() Method​
Each vars file must implement a call() method.
def call() {
stage('Build') {
sh 'mvn clean package'
}
}
Without call(), Jenkins cannot invoke the step.
Accepting Parameters​
Reusable steps can accept parameters like normal Groovy methods.
def call(Map config = [:]) {
def envName = config.get('env', 'qa')
echo "Running for ${envName}"
}
Usage:
buildAndTest(env: 'prod')
Using Pipeline Steps Inside vars​
Pipeline steps (sh, echo, stage, etc.) are available automatically.
def call() {
sh 'echo Running inside shared lib'
}
No imports required.
Accessing Environment Variables​
def call() {
echo "Build number: ${env.BUILD_NUMBER}"
}
Use env safely for read-only access.
Error Handling​
def call() {
try {
sh 'run-tests.sh'
} catch (e) {
error "Tests failed"
}
}
Use error() to fail the pipeline explicitly.
Common Mistakes​
- Putting too much logic in
vars - Forgetting
call()method - Hardcoding environment values
- Mixing credentials logic directly
Best Practices​
- Keep
varssteps small and readable - Delegate complex logic to
srcclasses - Use Map-based parameters for flexibility
- Version shared libraries
Interview Focus Areas​
- Why
call()is required - How
varssteps are loaded - Difference between
varsandsrcusage